﻿// Kevin Dockx, 11/2009 

using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using System.IO;
using System.Runtime.Serialization;
using System.Collections.Generic;
using System.Linq;

namespace WhatTheHelp.Core.Client.Repository
{
    public class IsolatedStorageRepository : IWhatTheHelpRepository
    {
        private const string IsolatedStorageKey = "WthIsolatedStorageKey";

        #region IWhatTheHelpRepository Members

        public void RetrieveHelp(HelpOverlayCriteria helpOverlayCriteria)
        {
            //Do we have someting stored in the isolated storage
            if (IsolatedStorageSettings.ApplicationSettings.Contains(IsolatedStorageKey))
            {
                //Get the overlay list from isolated storage
                var overlay = (List<HelpOverlay>)IsolatedStorageSettings.ApplicationSettings[IsolatedStorageKey];
                
                //Apply all filters optionally as they may not be defined.
                if (helpOverlayCriteria.ApplicationIdentifier != null)
                    overlay = overlay.Where(helpItem => helpItem.ApplicationIdentifier == helpOverlayCriteria.ApplicationIdentifier).ToList();
                if (helpOverlayCriteria.HelpOverlayIdentifier != null)
                    overlay = overlay.Where(helpItem => helpItem.HelpIdentifier.ToString() == helpOverlayCriteria.HelpOverlayIdentifier).ToList();
                if (helpOverlayCriteria.UserIdentifier != null)
                    overlay = overlay.Where(helpItem => helpItem.UserIdentifier == helpOverlayCriteria.UserIdentifier).ToList();
                if (helpOverlayCriteria.ViewAndStateIdentifier != null)
                    overlay = overlay.Where(helpItem => helpItem.ViewAndStateIdentifier == helpOverlayCriteria.ViewAndStateIdentifier).ToList();
                
                //Put the help overlays in the current localdatastore
                LocalDataStore.AddHelpOverlays(overlay);
            }            
        }

        public void StoreHelp(HelpOverlay helpOverlay)
        {
            List<HelpOverlay> overlays = new List<HelpOverlay>();
            //Do we have already have overlays stored in isolated storage
            if (IsolatedStorageSettings.ApplicationSettings.Contains(IsolatedStorageKey))
            {
                //Intialize the already existing overlays
                overlays = (List<HelpOverlay>)IsolatedStorageSettings.ApplicationSettings[IsolatedStorageKey];
            }
            //Add the current overlay to the in memory list
            overlays.Add(helpOverlay);
            //Store the list in the isolated storage
            IsolatedStorageSettings.ApplicationSettings[IsolatedStorageKey] = overlays;
        }

        #endregion
    }
}
